package com.itlong.cloud.controller.welink;

import com.itlong.cloud.POJO.DTO.miniprogram.FeedBackDTO;
import com.itlong.cloud.POJO.DTO.miniprogram.UpdateUserInfoDTO;
import com.itlong.cloud.POJO.PO.AppUserInfoPO;
import com.itlong.cloud.POJO.PO.FeedbackInfoPO;
import com.itlong.cloud.POJO.PO.WelinkBindRelationPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.app.AppUserCacheVO;
import com.itlong.cloud.POJO.VO.miniprogram.WelinkUserVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.MiniProgramErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.iottp.IIotTpSmsServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.miniprogram.IMiniProgramLoginServiceClient;
import com.itlong.cloud.user.miniprogram.IWelinkServiceClient;
import com.itlong.cloud.utils.encrypt.Md5EncryptUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      welink登录业务控制器
 * </desc>
 *
 * @createDate 2020/03/19
 */
@RestController
@RequestMapping("welink")
@RefreshScope
public class WelinkMineController {
    private static final Logger LOG = LoggerFactory.getLogger(WelinkMineController.class);

    @Value("${" + PlatformConstants.MINI_PROGRAM_REQUEST_PARAMS_ENCRYPT_KEY + "}")
    public String MINI_PROGRAM_REQUEST_PARAMS_ENCRYPT_KEY;

    @Autowired
    IWelinkServiceClient iWelinkServiceClient;

    @Autowired
    RedisService redisService;

    @Autowired
    IIotTpSmsServiceClient iIotTpSmsServiceClient;

    @Autowired
    IMiniProgramLoginServiceClient userCache;
    /**
     * <desc>
     *      产品反馈
     * </desc>
     *
     * @param sign
     * @return
     * @author Jiaqi.X
     * @createDate 2020/03/19
     */
    @PostMapping(path = "/mine/feedback")
    @ResponseBody
    public Object feedback(FeedBackDTO feedBackDTO,String sign,String token,HttpServletRequest request){
        try {
            token = request.getAttribute("token") == null ? null : request.getAttribute("token").toString();
            sign = request.getAttribute("sign") == null ? null : request.getAttribute("sign").toString();
            feedBackDTO= HttpProtocolUtil.parseRequestParamToDTO(FeedBackDTO.class, request);
            //验证签名
            if (!Md5EncryptUtil.checkAppSignValue(feedBackDTO, sign, MINI_PROGRAM_REQUEST_PARAMS_ENCRYPT_KEY)) {
                return new MessageVO(MiniProgramErrorCodeEnum.MINI_PROGRAM_SIGN_ERR.getErrorCode());
            }
            FeedbackInfoPO feedbackInfoPO = new FeedbackInfoPO();
            //从缓存中获取用户信息
            AppUserCacheVO userInfo=(AppUserCacheVO) StringHandlerUtil.formatString(userCache.retrieve(token));
            feedbackInfoPO.setUserId(userInfo.getUserId());
            feedbackInfoPO.setProjectId(feedBackDTO.getProjectId());
            feedbackInfoPO.setFeedbackInfo(feedBackDTO.getFeedBackInfo());
            feedbackInfoPO.setProblemType(feedBackDTO.getProblemType());
            feedbackInfoPO.setContactInformation(feedBackDTO.getContactInformation());
            iWelinkServiceClient.saveFeedBack(feedbackInfoPO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【WELINK feedback】产品反馈失败", e);
        }
    }

    /**
     * <desc>
     *      更新用户信息
     * </desc>
     *
     * @param sign
     * @return
     * @author Jiaqi.X
     * @createDate 2020/03/19
     */
    @PostMapping(path = "/mine/updateUserInfo")
    @ResponseBody
    public Object udpateUserInfo(UpdateUserInfoDTO updateUserInfoDTO,String sign,String token,HttpServletRequest request){
        try {
            token = request.getAttribute("token") == null ? null : request.getAttribute("token").toString();
            sign = request.getAttribute("sign") == null ? null : request.getAttribute("sign").toString();
            updateUserInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(UpdateUserInfoDTO.class, request);
            //验证签名
            if (!Md5EncryptUtil.checkAppSignValue(updateUserInfoDTO, sign, MINI_PROGRAM_REQUEST_PARAMS_ENCRYPT_KEY)) {
                return new MessageVO(MiniProgramErrorCodeEnum.MINI_PROGRAM_SIGN_ERR.getErrorCode());
            }
            AppUserInfoPO appUserInfoPO = new AppUserInfoPO();
            //从缓存中获取用户信息
            AppUserCacheVO userInfo=(AppUserCacheVO) StringHandlerUtil.formatString(userCache.retrieve(token));
            appUserInfoPO.setUserId(userInfo.getUserId());
            appUserInfoPO.setNickName(updateUserInfoDTO.getNickName());
            appUserInfoPO.setRealName(updateUserInfoDTO.getRealName());
            appUserInfoPO.setGender(updateUserInfoDTO.getGender());
            iWelinkServiceClient.updateUserInfo(appUserInfoPO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【WELINK feedback】产品反馈失败", e);
        }
    }

    /**
     * <desc>
     *      获取用户详情
     * </desc>
     *
     * @param sign
     * @return
     * @author Jiaqi.X
     * @createDate 2020/03/19
     */
    @PostMapping(path = "/mine/getUserInfo")
    @ResponseBody
    public Object getUserInfo(String sign,String token,HttpServletRequest request){
        try {
            token = request.getAttribute("token") == null ? null : request.getAttribute("token").toString();
            AppUserInfoPO appUserInfoPO = new AppUserInfoPO();
            //从缓存中获取用户信息
            AppUserCacheVO userInfo=(AppUserCacheVO) StringHandlerUtil.formatString(userCache.retrieve(token));
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iWelinkServiceClient.getUserInfo(userInfo.getUserId()));
        }catch (Exception e){
            throw new DataAccessException("【WELINK feedback】产品反馈失败", e);
        }
    }

}